<?php
class Controller_Test extends Controller_Abstract
{
	function actionIndex()
	{ 
	}
	
	function actionDT()
	{
		exit;
		if (isset($_GET['key']) && $_GET['key'] == Q::ini('custom_system/cron_key'))
		{
			//分销采购入库=>其他入库，需手动删除distribution和item的数据库数据
			$distribution = Distribution::find()->asArray()->getAll();
			foreach ($distribution as $d)
			{
				$distribution_item = Distribution_Item::find('distribution_id=?', $d['id'])->asArray()->getAll();
				$generate_result = Number_Rules::generate('other_io', 'QTRK');
				if ($generate_result['ack'] == SUCCESS)
				{
					//新增其他入库
					$new_other_io = new Other_IO();
					$new_other_io->number = $generate_result['number'];
					$new_other_io->type = Q::ini('custom_flag/other_io_type/in/value');
					$new_other_io->warehouse_id = 1;
					$new_other_io->create_time = $d['create_time'];
					$new_other_io->user_id = $d['create_user_id'];
					$new_other_io->finance_audit = $d['finance_audit'];
					$new_other_io->finance_audit_time = $d['finance_audit_time'];
					$new_other_io->finance_audit_user_id = $d['finance_audit_user_id'];
					$new_other_io->storage_audit = $d['storage_audit'];
					$new_other_io->storage_audit_user_id = $d['storage_audit_user_id'];
					$new_other_io->storage_audit_time = $d['storage_audit_time'];
					$new_other_io->notes = $d['notes'];
					$new_other_io->save();
					$new_other_io_id = mysql_insert_id();
					foreach ($distribution_item as $i)
					{
						$new_other_io_item = new Other_IO_Item();
						$new_other_io_item->other_io_id = $new_other_io_id;
						$new_other_io_item->product_basic_id = $i['product_basic_id'];
						$new_other_io_item->quantity = $i['quantity'];
						$new_other_io_item->cost_price = $i['cost_price'];
						$new_other_io_item->save();
						$new_other_io_item_id = mysql_insert_id();
						$inventory_log_dt = Inventory_Log::find('warehouse_id=? and type=? and relate_table_id=? and product_basic_id=?', 4, 12, $d['id'], $i['product_basic_id'])->getOne();
						$inventory_log_dt->warehouse_id = 1;
						$inventory_log_dt->type = Q::ini('custom_flag/inventory_log_type/other_in/value');
						$inventory_log_dt->relate_table_id = $new_other_io_item_id;
						$inventory_log_dt->save();
					}
				}
			}
			
			//销售出库type=Q::ini('custom_flag/inventory_log_type/sales/value')
			$distribution_sales_order = Sales_Order::find('warehouse_id=? and handle_status=?', 4, Q::ini('custom_flag/sales_order_handle_status/shipped/value'))->getAll();
			foreach ($distribution_sales_order as $do)
			{
				$item = Sales_Order_Item::find('sales_order_id=?', $do['id'])->asArray()->getAll();
				foreach ($item as $i)
				{
					//log记录的分销仓订单变成广州仓订单
					$inventory_log_sales = Inventory_log::find('warehouse_id=? and type=? and relate_table_id=? and product_basic_id=?', $do['warehouse_id'], Q::ini('custom_flag/inventory_log_type/sales/value'), $i['id'], $i['product_basic_id'])->getOne();
					$inventory_log_sales->warehouse_id = 1;
					$inventory_log_sales->save();
				}
				$do->warehouse_id = 1; //销售订单分销仓=>广州仓
				$do->save();
			}
			echo 'success, execute time:' . sprintf('%.4f', microtime(true) - $GLOBALS['g_boot_time']) . '(secs)';
		}
	}
	
	function actionWT()
	{
		exit;
		if (isset($_GET['key']) && $_GET['key'] == Q::ini('custom_system/cron_key'))
		{
			//WT入库=>采购
			$wt = Winit_Overseas_Instore::find()->asArray()->getAll();
			foreach ($wt as $w)
			{
				$new = new Purchase_Order();
				$new->number = $w['number'];
				$new->type = Q::ini('custom_flag/purchase_order_type/order/value');
				$new->warehouse_id = $w['warehouse_id'];
				$new->payment_type = $w['payment_type'];
				$new->purchase_user_id = $w['purchase_user_id'];
				$new->purchase_time = $w['purchase_time'];
				$new->notes = $w['notes'];
				$new->status = Q::ini('custom_flag/purchase_order_status/storaged/value');
				$new->make_user_id = $w['make_user_id'];
				$new->make_time = $w['make_time'];
				$new->purchase_audit_user_id = $w['confirmed_user_id'];
				$new->purchase_audit_time = $w['confirmed_time'];
				$new->complete_user_id = $w['confirmed_user_id'];
				$new->complete_time =  $w['confirmed_time'];
				$new->arrive_time = $w['storage_audit_time'];
				$new->arrive_user_id = $w['storage_audit_user_id'];
				$new->send_time = $w['confirmed_send_time'];
				$new->send_user_id = $w['confirmed_send_user_id'];
				$new->storage_audit_time = $w['arrived_time'];
				$new->storage_audit_user_id = $w['arrived_user_id'];
				$new->finance_audit = $w['finance_audit'];
				$new->finance_audit_time = $w['finance_audit_time'];
				$new->finance_audit_user_id = $w['finance_audit_user_id'];
				$new->save();
				$new_id = mysql_insert_id();
				
				//根据wt的id查到对应的item
				$item = Winit_Overseas_Instore_Item::find('winit_overseas_instore_id=?', $w['id'])->asArray()->getAll();
				foreach ($item as $i)
				{
					$new_item = new Purchase_Order_Item();
					$new_item->purchase_order_id = $new_id;
					$new_item->product_basic_id = $i['product_basic_id'];
					$new_item->actual_quantity = $i['quantity'];
					$new_item->actual_price = $i['price'];
					$new_item->actual_provider_id = $i['product_provider_id'];
					$new_item->audit_flag = Q::ini('custom_flag/purchase_order_item_audit_flag/audited/value');
					$new_item->save();
					$new_item_id = mysql_insert_id();
	
					//log表数据的相应调整
					$log = Inventory_Log::find('relate_table_id=? and warehouse_id=? and product_basic_id=? and type=?', $i['id'], $w['warehouse_id'], $i['product_basic_id'], Q::ini('custom_flag/inventory_log_type/winit_overseas_instore/value'))->getOne();
					$log->type = Q::ini('custom_flag/inventory_log_type/purchase/value');
					$log->relate_table_id = $new_item_id;
					$log->save();
				}
			}
		}
		echo 'success, execute time:' . sprintf('%.4f', microtime(true) - $GLOBALS['g_boot_time']) . '(secs)';
	}
	
	function actionDP()
	{
		exit;
		if (isset($_GET['key']) && $_GET['key'] == Q::ini('custom_system/cron_key'))
		{
			$fba = FBA_Allocate::find()->asArray()->getAll();
			foreach ($fba as $f)
			{
				if ($f['status'] == Q::ini('custom_flag/fba_allocate_status/stop/value'))
				{
					//强制终止调拨的调配单=>其他出库，共2记录
					$generate_result = Number_Rules::generate('other_io', 'QTCK');
					if ($generate_result['ack'] == SUCCESS)
					{
						$new_other_out = new Other_IO();
						$new_other_out->number = $generate_result['number'];
						$new_other_out->type = Q::ini('custom_flag/other_io_type/out/value');
						$new_other_out->warehouse_id = 1;
						$new_other_out->user_id = $f['make_user_id'];
						$new_other_out->create_time = $f['make_time'];
						$new_other_out->notes = $f['notes'];
						$new_other_out->storage_audit = Q::ini('custom_flag/other_io_storage_audit/audited/value');
						$new_other_out->save();
						$new_other_out_id = mysql_insert_id();
						
						//共27条item记录
						$item = FBA_Allocate_Item::find('fba_allocate_id=?', $f['id'])->asArray()->getAll();
						foreach ($item as $k => $i)
						{
							$new_other_io_item = new Other_IO_Item();
							$new_other_io_item->other_io_id = $new_other_out_id;
							$new_other_io_item->product_basic_id = $i['product_basic_id'];
							$new_other_io_item->quantity = $i['quantity'];
							$new_other_io_item->cost_price = $i['price'];
							$new_other_io_item->save();
							$new_other_io_item_id = mysql_insert_id();
							
							//log表type=9,gz_out_to_fba
							$gz_out_log = Inventory_Log::find('relate_table_id=? and product_basic_id=? and type=?', $i['id'], $i['product_basic_id'], Q::ini('custom_flag/inventory_log_type/gz_out_to_fba/value'))->getOne();
							$gz_out_log->type = Q::ini('custom_flag/inventory_log_type/other_out/value');
							$gz_out_log->relate_table_id = $new_other_io_item_id;
							$gz_out_log->save();
							
							if ($k == 0)
							{
								//取item第一条log的time和user_id作为other_io的仓库审核的time和user_id
								$s_time = $gz_out_log->time;
								$s_user_id = $gz_out_log->user_id;
							}
						}
						if ($s_time && $s_user_id)
						{
							QDB::getConn()->execute('update other_io set storage_audit_time=\'' . $s_time . '\', storage_audit_user_id=' . $s_user_id . ' where id=' . $new_other_out_id);
						}
					}
				}
				else
				{
					//正常走完FBA调拨流程的，包括调拨返回的，共22记录
					$new = new Warehouse_Allocate();
					$new->number = $f['number'];
					$new->status = $f['status'];
					$new->start_warehouse_id = 1;
					$new->end_warehouse_id = $f['warehouse_id'];
					$new->make_user_id = $f['make_user_id'];
					$new->make_time = $f['make_time'];
					$new->notes = $f['notes'];
					$new->save();
					$new_id = mysql_insert_id();
					
					//共217条item记录，log对应有217*2=434条记录
					$item = FBA_Allocate_Item::find('fba_allocate_id=?', $f['id'])->asArray()->getAll();
					foreach ($item as $k => $i)
					{
						$new_item = new Warehouse_Allocate_Item();
						$new_item->warehouse_allocate_id = $new_id;
						$new_item->product_basic_id = $i['product_basic_id'];
						$new_item->quantity = $i['quantity'];
						$new_item->price = $i['price'];
						$new_item->save();
						$new_item_id = mysql_insert_id();
						
						//log表type=9,gz_out_to_fba
						$gz_out_log = Inventory_Log::find('relate_table_id=? and product_basic_id=? and type=?', $i['id'], $i['product_basic_id'], Q::ini('custom_flag/inventory_log_type/gz_out_to_fba/value'))->getOne();
						if ($gz_out_log['id'])
						{
							$gz_out_log->type = Q::ini('custom_flag/inventory_log_type/allocate_out/value');
							$gz_out_log->relate_table_id = $new_item_id;
							$gz_out_log->save();
							if ($k == 0)
							{
								$c_time = $gz_out_log['time'];
								$c_user_id = $gz_out_log['user_id'];
							}
						}
						
						//log表type=10,fba_in_from_gz
						$fba_in_log = Inventory_Log::find('relate_table_id=? and product_basic_id=? and type=?', $i['id'], $i['product_basic_id'], Q::ini('custom_flag/inventory_log_type/fba_in_from_gz/value'))->getOne();
						if ($fba_in_log['id'])
						{
							$fba_in_log->type = Q::ini('custom_flag/inventory_log_type/allocate_in/value');
							$fba_in_log->relate_table_id = $new_item_id;
							$fba_in_log->save();
							if ($k == 0)
							{
								$a_time = $fba_in_log['time'];
								$a_user_id = $fba_in_log['user_id'];
							}
						}
						
						//log表type=11,gz_in_from_fba
						$gz_in_from_fba = Inventory_Log::find('relate_table_id=? and product_basic_id=? and type=?', $i['id'], $i['product_basic_id'], Q::ini('custom_flag/inventory_log_type/gz_in_from_fba/value'))->getOne();
						if ($gz_in_from_fba['id'])
						{
							$gz_in_from_fba->type = Q::ini('custom_flag/inventory_log_type/allocate_return/value');
							$gz_in_from_fba->relate_table_id = $new_item_id;
							$gz_in_from_fba->save();
							if ($k == 0)
							{
								$r_time = $gz_in_from_fba['time'];
								$r_user_id = $gz_in_from_fba['user_id'];
							}
						}
					}
					
					//更新item[0]的log记录回warehouse_allocate表对应的字段
					if ($f['status'] == Q::ini('custom_flag/fba_allocate_status/return/value'))
					{
						QDB::getConn()->execute('update warehouse_allocate set confirm_user_id=' . $c_user_id . ', confirm_time=\'' . $c_time . '\', return_user_id=' . $r_user_id . ', return_time=\'' . $r_time . '\' where id=' . $new_id);
					}
					else
					{
						QDB::getConn()->execute('update warehouse_allocate set confirm_user_id=' . $c_user_id . ', confirm_time=\'' . $c_time . '\', arrive_user_id=' . $a_user_id . ', arrive_time=\'' . $a_time . '\' where id=' . $new_id);
					}
				}
			}
			echo 'success, execute time:' . sprintf('%.4f', microtime(true) - $GLOBALS['g_boot_time']) . '(secs)';
		}
	}
	
	function actionDF()
	{
		exit;
		//DF入库单全部入库才能执行
		if (isset($_GET['key']) && $_GET['key'] == Q::ini('custom_system/cron_key'))
		{
			$df = DF_Overseas_Instore::find()->asArray()->getAll();
			foreach ($df as $d)
			{
				$new = new Purchase_Order();
				$new->number = $d['number'];
				$new->type = Q::ini('custom_flag/purchase_order_type/order/value');;
				$new->payment_type = $d['payment_type'];
				$new->warehouse_id = $d['warehouse_id'];
				$new->purchase_user_id = $d['purchase_user_id'];
				$new->purchase_time = $d['purchase_time'];
				$new->notes = $d['notes'];
				$new->actual_amount = QDB::getConn()->execute('select sum(quantity*price) from df_overseas_instore_item where df_overseas_instore_id=' . $d['id'])->fetchOne();
				$new->status = Q::ini('custom_flag/df_overseas_instore_status/arrived/value');
				$new->make_user_id = $d['make_user_id'];
				$new->make_time = $d['make_time'];
				$new->purchase_audit_user_id = $d['confirmed_user_id'];
				$new->purchase_audit_time = $d['confirmed_time'];
				$new->complete_user_id = $d['confirmed_user_id'];
				$new->complete_time =  $d['confirmed_time'];
				$new->arrive_time = $d['storage_audit_time'];
				$new->arrive_user_id = $d['storage_audit_user_id'];
				$new->send_time = $d['confirmed_send_time'];
				$new->send_user_id = $d['confirmed_send_user_id'];
				$new->storage_audit_time = $d['arrived_time'];
				$new->storage_audit_user_id = $d['arrived_user_id'];
				$new->finance_audit = $d['finance_audit'];
				$new->finance_audit_time = $d['finance_audit_time'];
				$new->finance_audit_user_id = $d['finance_audit_user_id'];
				$new->save();
				$new_id = mysql_insert_id();
				$item = DF_Overseas_Instore_Item::find('df_overseas_instore_id=?', $d['id'])->asArray()->getAll();
				foreach ($item as $i)
				{
					$new_item = new Purchase_Order_Item();
					$new_item->purchase_order_id = $new_id;
					$new_item->product_basic_id = $i['product_basic_id'];
					$new_item->actual_quantity = $i['quantity'];
					$new_item->actual_price = $i['price'];
					$new_item->actual_provider_id = $i['product_provider_id'];
					$new_item->audit_flag = Q::ini('custom_flag/purchase_order_item_audit_flag/audited/value');
					$new_item->save();
					$new_item_id = mysql_insert_id();
					//log表数据的相应调整
					$log = Inventory_Log::find('relate_table_id=? and warehouse_id=? and product_basic_id=? and type=?', $i['id'], $d['warehouse_id'], $i['product_basic_id'], Q::ini('custom_flag/inventory_log_type/df_overseas_instore/value'))->getOne();
					$log->type = Q::ini('custom_flag/inventory_log_type/purchase/value');
					$log->relate_table_id = $new_item_id;
					$log->save();
				}
			}
			echo 'success, execute time:' . sprintf('%.4f', microtime(true) - $GLOBALS['g_boot_time']) . '(secs)';exit;
		}
	}
	
	function actionFBA()
	{
		exit;
		//注意：fba全部入库才能执行
		if (isset($_GET['key']) && $_GET['key'] == Q::ini('custom_system/cron_key'))
		{
			$fba = FBA_Overseas_Instore::find()->asArray()->getAll();
			foreach ($fba as $f)
			{
				$generate_result = Number_Rules::generate('purchase_order', 'CG');
				if ($generate_result['ack'] == SUCCESS)
				{
					$new = new Purchase_Order();
					$new->number = $generate_result['number'];
					$new->type = Q::ini('custom_flag/purchase_order_type/order/value');;
					$new->payment_type = $f['payment_type'];
					$new->warehouse_id = $f['warehouse_id'];
					$new->purchase_user_id = $f['purchase_user_id'];
					$new->purchase_time = $f['purchase_time'];
					$new->notes = $f['notes'];
					$new->actual_amount = QDB::getConn()->execute('select sum(quantity*price) from fba_overseas_instore_item where fba_overseas_instore_id=' . $f['id'])->fetchOne();
					$new->status = Q::ini('custom_flag/purchase_order_status/storaged/value');
					$new->make_user_id = $f['make_user_id'];
					$new->make_time = $f['make_time'];
					$new->purchase_audit_user_id = $f['confirmed_user_id'];
					$new->purchase_audit_time = $f['confirmed_time'];
					$new->complete_user_id = $f['confirmed_user_id'];
					$new->complete_time =  $f['confirmed_time'];
					$new->arrive_time = $f['storage_audit_time'];
					$new->arrive_user_id = $f['storage_audit_user_id'];
					$new->send_time = $f['confirmed_send_time'];
					$new->send_user_id = $f['confirmed_send_user_id'];
					$new->storage_audit_time = $f['arrived_time'];
					$new->storage_audit_user_id = $f['arrived_user_id'];
					$new->finance_audit = $f['finance_audit'];
					$new->finance_audit_time = $f['finance_audit_time'];
					$new->finance_audit_user_id = $f['finance_audit_user_id'];
					$new->save();
					$new_id = mysql_insert_id();
					$item = FBA_Overseas_Instore_Item::find('fba_overseas_instore_id=?', $f['id'])->asArray()->getAll();
					foreach ($item as $i)
					{
						$new_item = new Purchase_Order_Item();
						$new_item->purchase_order_id = $new_id;
						$new_item->product_basic_id = $i['product_basic_id'];
						$new_item->actual_quantity = $i['quantity'];
						$new_item->actual_price = $i['price'];
						$new_item->actual_provider_id = $i['product_provider_id'];
						$new_item->audit_flag = Q::ini('custom_flag/purchase_order_item_audit_flag/audited/value');
						$new_item->save();
						$new_item_id = mysql_insert_id();
						//log表数据的相应调整
						$log = Inventory_Log::find('relate_table_id=? and warehouse_id=? and product_basic_id=? and type=?', $i['id'], $f['warehouse_id'], $i['product_basic_id'], Q::ini('custom_flag/inventory_log_type/fba_overseas_instore/value'))->getOne();
						$log->type = Q::ini('custom_flag/inventory_log_type/purchase/value');
						$log->relate_table_id = $new_item_id;
						$log->save();
					}
				}
			}
			echo 'success, execute time:' . sprintf('%.4f', microtime(true) - $GLOBALS['g_boot_time']) . '(secs)';exit;
		}
	}
}